/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年11月17日 10:08:50
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.DashLayout;
import com.pactera.jep.sys.service.DashLayoutService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.controller.CrudController;
import com.pactera.jep.web.entity.NameValuePair;
import com.pactera.jep.web.util.NVUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(value="布局管理")
@RestController
@RequestMapping("/dashLayout")
public class DashLayoutRestController extends CrudController<DashLayout>{
	private static Logger	logger	= LoggerFactory.getLogger(DashLayoutRestController.class);

	@Autowired
	private DashLayoutService	dashLayoutService;


	
	/**
	 * 为列表页面做数据准备
	 */
	@ApiOperation("为列表页面做数据准备")
	@Override
	protected void prepareQuery(Page<DashLayout> page, List<Filter> filters, List<Order> orders,HttpServletRequest request) {
		orders.add(Order.desc("priority"));
		dashLayoutService.query(page, filters, orders);
	}



	/**
	 * 保存
	 * @param dashLayout
	 * @return
	 */
	@ApiOperation("保存")
	@PostMapping(value = "/save")
	public Map<String, Object> save(@RequestBody  DashLayout dashLayout) {
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		Boolean isExist = dashLayoutService.exists(dashLayout.getCode());
		try 
		{
			if(isExist)
			{
				
				dashLayoutService.update(dashLayout);
			}
			else
			{
				dashLayoutService.insert(dashLayout);
			}
			
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}
	
	/**
	 * 删除
	 * @param code
	 * @return
	 */
	@ApiOperation("删除")
	@PostMapping(value = "/remove/{code}")
	public Map<String, Object> remove(@PathVariable String code){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try 
		{
			dashLayoutService.deleteByPrimaryKey(code);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} 
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		
		return tips;
	}
	
	/**
	 * 验证提交的code是否已经存在数据库中
	 * @param code
	 * @return
	 */
	@ApiOperation("验证提交的code是否已经存在数据库中")
	@RequestMapping(value = "/check", produces = "application/json;charset=UTF-8")
	public Map<String, Object> check(String code){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		boolean exist = dashLayoutService.exists(code);
		tips.put("valid", !exist);
		
		return tips;
	}

	@ApiOperation("获取所有的布局")
	@RequestMapping("/getAll")
	public List<NameValuePair> getAll(){
		List<DashLayout> dashLayouts = new ArrayList<DashLayout>();
		try {
			dashLayouts=dashLayoutService.selectAll();
			DashLayout dashLayout=new DashLayout();
			dashLayout.setCode("_RESET_DELETE_");
			dashLayout.setName("[取消关联，重置为空]");
			dashLayouts.add(dashLayout);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
		return NVUtils.convert(dashLayouts, dashLayouts.size(),"code","name");
	}

}